package com.acme.bankapp.domain.bank;

import com.acme.bankapp.service.bank.AccountDoesntExistsException;

public class Client {
	private String name;
	private Account account;
	private Gender gender;

	public String getClientSalutation() {
		return gender.getSalutation() + getName();
	}

	public Client() {
	}

	public Client(String name, Gender gender, Account account) {
		this.account = account;
		this.gender = gender;
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public Gender getGender() {
		return gender;
	}

	public Account getAccount() throws AccountDoesntExistsException {
		if (account == null)
			throw new AccountDoesntExistsException(
					"Failure! Tried to get missing account");
		return account;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void addAccount(Account account) {
		this.account = account;
		System.out.println("New " + account.getClass().getSimpleName()
				+ " for client added");
	}

	@Override
	public String toString() {
		StringBuffer sbuf = new StringBuffer("Client ");
		sbuf.append(gender.getSalutation() + name);
		if (account != null){
		sbuf.append(", account is: " + account.getClass().getSimpleName());
		sbuf.append(", balance is: " + account);
		} else {
	    sbuf.append(", client have no accounts");
		}
		sbuf.append(", gender is "+ gender);
		return sbuf.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((account == null) ? 0 : account.hashCode());
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		Client other = (Client) obj;
		if (account == null) {
			if (other.account != null) return false;
		} else if (!account.equals(other.account)) return false;
		if (gender != other.gender) return false;
		if (name == null) {
			if (other.name != null) return false;
		} else if (!name.equals(other.name)) return false;
		return true;
	}
	
}
